package com.xmlt.core.template.instance;

import org.w3c.dom.Element;

import com.xmlt.core.template.TemplateManager;
import com.xmlt.core.template.TemplateParameter;
import com.xmlt.core.template.types.IValidator;
import com.xmlt.core.template.types.StringValidator;
import com.xmlt.core.template.types.ValidatorRegistry;

public class Value extends AbstractInstance {
	protected IValidator validator;

	public Value(Element el, TemplateManager tm, TemplateParameter tp,
			AbstractInstance parent) {
		super(el, tm, tp, parent);
		String ref = tp.getReference();
		if (ref == null || "".equals(ref)) {
			validator = new StringValidator();
		} else {
			validator = ValidatorRegistry.getValidator(ref);
			if (validator == null)
				validator = new StringValidator();
		}
	}

	@Override
	public AbstractInstance lookup(String path) {
		return null;
	}

	public String getDisplayValue() {
		return validator.getDisplayValue(getElement().getTextContent());
	}

	public Object getValue() {
		return validator.getContentValue(getElement().getTextContent());
	}

	public void setValue(String value) {
		getElement()
				.setTextContent(validator.getContentValue(value).toString());
	}

	public boolean validate() {
		return validator.validate(getElement().getTextContent());
	}

	public IValidator getValidator() {
		return validator;
	}

}
